package com.ssm.basis.web.controller;

import com.ssm.basis.service.util.MessageHelper;
import com.ssm.basis.service.util.SsmContextHolder;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;

import static com.ssm.basis.service.constant.SecurityConstant.HEADER_X_AUTH_TOKEN;
import static java.util.Collections.EMPTY_MAP;
import static java.util.Objects.nonNull;
import static org.springframework.web.bind.annotation.RequestMethod.*;

@Hidden
@RestController
@RequestMapping
@Tag(name = "登录登出及权限处理")
public class WebSecurityController {
    @Operation(summary = "登录成功处理")
    @PostMapping("login/success")
    public Object loginSuccess(HttpSession session, @RequestAttribute(required = false) Authentication auth) {
        SsmContextHolder.setMessage(MessageHelper.getMessage("msg[0]"));
        // https://blog.csdn.net/f641385712/article/details/100120763
        Map<String, Object> map = CollectionUtils.newHashMap(3);
        map.put("tokenHead", HEADER_X_AUTH_TOKEN);
        map.put("tokenValue", session.getId());
        map.put("authentication", auth);
        return map;
    }

    @Operation(summary = "登录失败处理")
    @PostMapping("login/failure")
    public Object loginFailure(@RequestAttribute(required = false) Exception ex) {
        SsmContextHolder.setMessage(SpringSecurityMessageSource.getAccessor()
                .getMessage("BindAuthenticator.badCredentials", "Invalid credentials")
        );
        if (nonNull(ex)) {
            ex.printStackTrace();
            return ex;
        }
        return EMPTY_MAP;
    }

    @Operation(summary = "登出成功处理")
    @GetMapping("logout/success")
    public Object logoutSuccess(@RequestAttribute(required = false) Authentication auth) {
        SsmContextHolder.setMessage(MessageHelper.getMessage("msg[1]"));
        return nonNull(auth) ? auth : EMPTY_MAP;
    }

    @Operation(summary = "未登录处理")
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @RequestMapping(path = "access/authentication", method = {POST, DELETE, PUT, GET})
    public Object accessAuthentication(@RequestAttribute(required = false) Exception ex) {
        SsmContextHolder.setMessage(MessageHelper.getMessage("msg[2]"));
        if (nonNull(ex)) {
            ex.printStackTrace();
            return ex;
        }
        return EMPTY_MAP;
    }

    @Operation(summary = "访问无权限处理")
    @ResponseStatus(HttpStatus.FORBIDDEN)
    @RequestMapping(path = "access/denied", method = {POST, DELETE, PUT, GET})
    public Object accessDenied(@RequestAttribute(required = false) Exception ex) {
        SsmContextHolder.setMessage(MessageHelper.getMessage("msg[3]"));
        if (nonNull(ex)) {
            ex.printStackTrace();
            return ex;
        }
        return EMPTY_MAP;
    }
}
